# Copyright (c) 2009 Peter Corbett
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.

import colours

directions = ((0,-1), (1,-1), (1,0), (1,1), (0,1), (-1,1), (-1,0), (-1,-1))

class IDMaker:
    def __init__(self):
        self.id = 0

    def new_id(self):
        self.id += 1
        return self.id

def scale_speed(speed):
    return max(1, int(10 * (2.0 ** (-speed / 10.0))))

def rotate(t, steps):
    if t not in directions:
        return None
    return directions[(directions.index(t) + 8 - steps) % 8]

def add_dict(to_dict, from_dict):
    for k in from_dict:
        if k not in to_dict:
            to_dict[k] = 0
        to_dict[k] += from_dict[k]

def firstupper(s):
    return s[0].upper() + s[1:]

def fract_colour(v, maxv):
    return [colours.GREEN, colours.YELLOW, colours.YELLOW, colours.RED][min(3, max(0, ((maxv - v) * 4) / maxv))]
